package com.example.databasehomework;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class DetailListAdapter extends RecyclerView.Adapter {

    public Context context;
    public List<DetailListBean> data;
    public OnItemClickListener itemClickListener;
    public Activity_ExpInfo expInfo;

    public DetailListAdapter(Context context) {
        this.context = context;
    }

    public DetailListAdapter(Context context, List<DetailListBean> data, Activity_ExpInfo eInfo){
        this.context=context;
        this.data=data;
        this.expInfo = eInfo;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.detail_list_item,parent,false);
        return new MyViewHolder(view,itemClickListener);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        TextView seatNum = ((DetailListAdapter.MyViewHolder)holder).v.findViewById(R.id.exp_list_item_seatnum);
        seatNum.setText(Integer.toString(data.get(position).seatNum));
        TextView name = ((DetailListAdapter.MyViewHolder)holder).v.findViewById(R.id.exp_list_item_name);
        name.setText(data.get(position).name);
        TextView sNumber = ((DetailListAdapter.MyViewHolder)holder).v.findViewById(R.id.exp_list_item_snumber);
        sNumber.setText("#" + data.get(position).sNumber);
        ImageView img = ((DetailListAdapter.MyViewHolder)holder).v.findViewById(R.id.exp_list_item_img);
        if (data.get(position).ifSignIn) {
            img.setImageResource(R.drawable.ifsignin_main_dark);
        } else {
            img.setImageResource(0);
        }
        TextView swipe = ((DetailListAdapter.MyViewHolder)holder).v.findViewById(R.id.swipe_text);
        swipe.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                expInfo.signIn(data.get(position - 1).sNumber, !data.get(position - 1).ifSignIn);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private OnItemClickListener onItemClickListener;
        private View v;

        public MyViewHolder(View itemView,OnItemClickListener onItemClickListener) {
            super(itemView);
            this.onItemClickListener=onItemClickListener;
            itemView.setOnClickListener(this);
            v=itemView;
        }

        @Override
        public void onClick(View view) {
            if(onItemClickListener!=null)
                onItemClickListener.onItemClick(v,getAdapterPosition());
        }
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.itemClickListener=onItemClickListener;
    }

}
